package com.example.lettcode._202411._20241122;

import java.util.*;

/*
500. 键盘行
给你一个字符串数组 words ，只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
请注意，字符串 不区分大小写，相同字母的大小写形式都被视为在同一行。
美式键盘 中：

第一行由字符 "qwertyuiop" 组成。
第二行由字符 "asdfghjkl" 组成。
第三行由字符 "zxcvbnm" 组成。
American keyboard


示例 1：
输入：words = ["Hello","Alaska","Dad","Peace"]
输出：["Alaska","Dad"]
解释：
由于不区分大小写，"a" 和 "A" 都在美式键盘的第二行。

示例 2：
输入：words = ["omk"]
输出：[]

示例 3：
输入：words = ["adsdf","sfd"]
输出：["adsdf","sfd"]

提示：
1 <= words.length <= 20
1 <= words[i].length <= 100
words[i] 由英文字母（小写和大写字母）组成
 */
public class _02_jian_pan_hang {

    public static void main(String[] args) {
        String[] words = {"Hello","Alaska","Dad","Peace"};
        String[] words1 = findWords2(words);
        Arrays.stream(words1).forEach(System.out::println);
    }

    public static String[] findWords(String[] words) {
        String first = "qwertyuiop";
        String second = "asdfghjkl";
        String three = "zxcvbnm";
        Map<Character, Integer> map = new HashMap<>();
        char[] firstChar = first.toCharArray();
        for (char c : firstChar) {
            map.put(c, 1);
        }
        char[] secondChar = second.toCharArray();
        for (char c : secondChar) {
            map.put(c, 2);
        }

        char[] threeChar = three.toCharArray();
        for (char c : threeChar) {
            map.put(c, 3);
        }

        List<String> retList = new ArrayList<>();
        for (String word : words) {
            char[] wordChars = word.toLowerCase(Locale.ROOT).toCharArray();
            Integer ints = map.get(wordChars[0]);
            boolean flag = true;
            for (int i = 1; i < wordChars.length; i++) {
                if (ints != map.get(wordChars[i])) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                retList.add(word);
            }
        }
        return retList.toArray(new String[retList.size()]);
    }


    public static String[] findWords2(String[] words) {
        List<String> list = Arrays.asList("qwertyuiop", "asdfghjkl", "zxcvbnm");
        List<String> retList = new ArrayList<>();
        for (String word : words) {
            int index = 0;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).contains(String.valueOf(word.charAt(0)).toLowerCase())) {
                    index = i;
                    break;
                }
            }
            int i = 0;
            for ( ; i < word.length(); i++) {
                if (!list.get(index).contains(String.valueOf(word.charAt(i)).toLowerCase())) {
                    break;
                }
            }

            if (i == word.length()) {
                retList.add(word);
            }
        }
        return retList.toArray(new String[retList.size()]);
    }
}
